import { createRouter, createWebHistory } from "vue-router";
const importRoute = import.meta.glob("../views/**/**.vue");
const routes = [
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/",
    component: () => import("@/components/MainLayout.vue"),
    name: "MainLayout",
    children: [
      // {
      //   path: "/index",
      //   name: "首页",
      //   component: () => import("../views/home/index.vue"),
      //   meta: { icon: "House", showInSidebar: true },
      // },
      // {
      //   path: "enterprise",
      //   name: "企业管理xxx",
      //   meta: { icon: "Odometer", showInSidebar: true },
      //   children: [
      //     {
      //       path: "enterpriseCertification",
      //       name: "企业认证xxx",
      //       meta: { icon: "Odometer", showInSidebar: false },
      //       component: () => import("../views/enterprise/enterpriseCertification/index.vue")
      //     }, 
      //     {
      //       path: "enterpriseManagement",
      //       name: "企业管理xxx",
      //       meta: { icon: "Odometer", showInSidebar: false },
      //       component: () => import("../views/enterprise/enterpriseManagement/index.vue")
      //     },
      //     {
      //       path: "myEnterprise",
      //       name: "我的企业",
      //       meta: { icon: "Odometer", showInSidebar: false },
      //       component: () => import("../views/enterprise/myEnterprise.vue")
      //     },
      //   ]
      // },

      // {
      //   path: "/balance/withdrawalChannels",
      //   name: "提现渠道",
      //   meta: { icon: "Odometer", showInSidebar: true },
      //   component: () => import("../views/balance/withdrawalChannels/index.vue"),
      // },
      {
        path: "/clockln/examine/detail",
        name: "查看详情",
        meta: { icon: "Odometer", showInSidebar: false },
        component: () => import("../views/clockln/examineDetail.vue"),
      },
      {
        path: "/operate/recommendation/add",
        name: "创建推荐",
        meta: { icon: "Odometer", showInSidebar: false },
        component: () => import("../views/operate/recommendation/add.vue"),
      },
      {
        path: "/operate/wxapp/homeMenu/add",
        name: "新增菜单",
        meta: { icon: "Odometer", showInSidebar: false },
        component: () => import("../views/operate/wxapp/homeMenu/add.vue"),
      },
     
      {
        path: "/enterprise/son/add",
        name: "创建子企业",
        meta: { icon: "Odometer", showInSidebar: false },
        component: () => import("../views/enterprise/son/add.vue"),
      },
      {
        path: "/position/add",
        name: "职位详情",
        meta: { icon: "Odometer", showInSidebar: false },
        component: () => import("../views/position/add.vue"),
      },
      {
        path: "/user/seedUserDetails",
        name: "创客用户详情",
        meta: { icon: "Odometer", showInSidebar: false },
        component: () => import("../views/user/seedUserDetails.vue"),
      },
      {
        path: "/clockinDetail",
        name: "打卡详情",
        component: () => import("@/views/clockln/clockinDetail.vue"),
        meta: { icon: "House", showInSidebar: false },
      },
      {
        path: "/balance/enterpriseBillDetails",
        name: "账单详情",
        component: () => import("@/views/balance/enterpriseBillDetails.vue"),
        meta: { icon: "House", showInSidebar: false },
      },
      {
        path: "/balance/capitalDetails",
        name: "资金详情",
        component: () => import("@/views/balance/capitalDetails.vue"),
        meta: { icon: "House", showInSidebar: false },
      },
      {
        path: "/balance/enterpriseBill",
        name: "查看账单",
        component: () => import("@/views/balance/enterpriseBill.vue"),
        meta: { icon: "House", showInSidebar: false },
      },
      {
        path: "/enterprise/enterpriseManagement/add",
        name: "创建/编辑企业",
        component: () => import("@/views/enterprise/enterpriseManagement/add.vue"),
        meta: { icon: "House", showInSidebar: false },
      },
      {
        path: "/roleAdd",
        name: "添加/编辑管理",
        component: () => import("@/views/role/add.vue"),
        meta: { icon: "House", showInSidebar: false },
      },
      {
        path: "/activeUsersDetail",
        name: "用户详情",
        component: () => import("@/views/user/activeUsersDetail.vue"),
        meta: { icon: "House", showInSidebar: false },
      },
      {
        path: "/operate/shareSet/posterDispose",
        name: "分享海报配置",
        component: () => import("@/views/operate/shareSet/posterDispose.vue"),
        meta: { icon: "House", showInSidebar: false },
      },
      {
        path: "/operate/shareSet/TextDispose",
        name: "分享文案配置",
        component: () => import("@/views/operate/shareSet/TextDispose.vue"),
        meta: { icon: "House", showInSidebar: false },
      },

    ],
  },
];

export const parseDynamicRoutes = (dynamicRoutes) => {
  const parseRoutes = (routes) => {
    return routes.map((route) => {
      const parsedRoute = {
        path: route.path,
        name: route.name,
        meta: route.meta,
      };
      if (route.components) {
        parsedRoute.component = importRoute[`../views${route.components}`];
      }
      if (route.children && route.children.length > 0) {
        parsedRoute.children = parseRoutes(route.children);
      }
      return parsedRoute;
    });
  };

  return parseRoutes(dynamicRoutes);
};
const router = createRouter({
  history: createWebHistory(),
  routes: routes,
});
export const addDynamicRoutes = (dynamicRoutes) => {
  const parsedRoutes = parseDynamicRoutes(dynamicRoutes);
  parsedRoutes.forEach((route,index) => {
    console.log(index,'index',route);
    router.addRoute("MainLayout", route);
  });
  console.log(parsedRoutes);
};
router.beforeEach(async (to, _from, next) => {
  const isAuthenticated = localStorage.getItem("token");
  if (!isAuthenticated && to.path !== "/login") {
    next("/login");
    return;
  }
  next();
});
export default router;
